//
//  CircleButton.m
//  etionUI
//
//  Created by wangjian on 12/9/13.
//  Copyright (c) 2013 GuangZhouXuanWu. All rights reserved.
//

#import "CircleButton.h"

#define CB_MARGIN 5

@interface CCircleButton ()

@property (nonatomic, retain) UIImage *normalImage;
@property (nonatomic, retain) UIImage *highlightedImage;
@property (nonatomic, assign) CGFloat radius;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, assign) ECircleButtonTitlePosition titlePosition;
@property (nonatomic, assign) UILabel *titleLable;
@property (nonatomic, assign) CGRect circleFrame;

@end

@implementation CCircleButton
{
    UIImageView *_imageView;
}

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.opaque = NO;
        self.radius = self.width/2;
        self.circleFrame = self.bounds;
    }
    return self;
}

- (void)dealloc
{
    [_normalImage release];
    [_highlightedImage release];
    [_title release];
    superdealloc_mm;
}

- (id)initWithFrame:(CGRect)frame normalImage:(UIImage *)normalImage highlightedImage:(UIImage *)highlightedImage
{
    if (self = [self initWithNormalImage:normalImage highlightedImage:highlightedImage radius:frame.size.width/2 title:nil titlePosition:ECircleButtonTitlePositionLeft])
    {
        self.frame = frame;
    }
    return self;
}

- (id)initWithNormalImage:(UIImage *)normalImage highlightedImage:(UIImage *)highlightedImage radius:(CGFloat)radius title:(NSString *)title titlePosition:(ECircleButtonTitlePosition)titlePosition
{
    if (self = [self initWithFrame:CGRectMake(0, 0, 2*radius, 2*radius)])
    {
        self.normalImage = normalImage;
        self.highlightedImage = highlightedImage;
        self.radius = radius;
        self.title = title;
        self.titlePosition = titlePosition;
        
        if (self.title.length > 0)
        {
            if (self.titleLable == nil)
            {
                self.titleLable = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
                [self addSubview:self.titleLable];
            }
            
            CGSize titleSize = [self.title stringSizeWithFont:[UIFont systemFontOfSize:12]];
            titleSize.width += 2*CB_MARGIN;
            
            if (self.titlePosition == ECircleButtonTitlePositionLeft)
            {
//                self.frame = CGRectMake(0, 0, titleSize.width+CB_MARGIN+2*self.radius, 2*self.radius);
                self.circleFrame = CGRectMake(0, 0, 2*self.radius, 2*self.radius);
                self.titleLable.frame = CGRectMake(-titleSize.width-CB_MARGIN, (self.height-titleSize.height)/2, titleSize.width, titleSize.height);//CGRectMake(0, (self.height-titleSize.height)/2, titleSize.width, titleSize.height);
            }
            else
            {
                self.frame = CGRectMake(0, 0, MAX(titleSize.width, 2*self.radius), titleSize.height+CB_MARGIN+2*self.radius);
                self.circleFrame = CGRectMake(self.width/2-self.radius, 0, 2*self.radius, 2*self.radius);
                self.titleLable.frame = CGRectMake(titleSize.width<2*self.radius ? (2*self.radius-titleSize.width)/2 : 0, 2*self.radius+CB_MARGIN, titleSize.width, titleSize.height);
            }
            
            self.titleLable.textAlignment = NSTextAlignmentCenter;
            self.titleLable.layer.cornerRadius = titleSize.height/2;
            self.titleLable.backgroundColor = [UIColor colorWithWhite:.1 alpha:.5];
            self.titleLable.textColor = [UIColor whiteColor];
            self.titleLable.font = [UIFont systemFontOfSize:12];
            self.titleLable.text = self.title;
            [self addSubview:self.titleLable];
        }
        
        if (_imageView == nil)
        {
            _imageView = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
            [self addSubview:_imageView];
        }
        [self updateImage];
    }
    return self;
}

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    [self setNeedsDisplay];
    [self updateImage];
}

- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
    [self setNeedsDisplay];
    [self updateImage];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:.9 alpha:.7].CGColor);
    CGContextFillEllipseInRect(context, self.circleFrame);
    if (self.highlighted == YES || self.selected == YES)
    {
        CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
        CGContextSetLineWidth(context, 1);
        CGContextStrokeEllipseInRect(context, CGRectInset(self.circleFrame, 1, 1));
    }
}

- (void)updateImage
{
    UIImage *image = nil;
    if ((self.highlighted == YES || self.selected == YES) && self.highlightedImage != nil)
    {
        image = self.highlightedImage;
    }
    else
    {
        image = self.normalImage;
    }
    _imageView.image = image;
    _imageView.frame = CGRectInset(self.circleFrame, (self.circleFrame.size.width-image.size.width)/2, (self.circleFrame.size.height-image.size.height)/2);
}

- (void)setTitleState:(BOOL)isShow animated:(BOOL)animated
{
    if (self.titleLable == nil)
    {
        return;
    }
    
    void (^fun)() = ^()
    {
        self.titleLable.alpha = isShow ? 1.0 : 0.0;
    };
    
    if (animated == NO)
    {
        fun();
    }
    else
    {
        [UIView animateWithDuration:.25 animations:^{
            fun();
        } completion:^(BOOL finished) {
            
        }];
    }
}

@end

@interface CCircleColorView : UIView

@property (nonatomic, retain) UIColor *color;

@end

@implementation CCircleColorView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.opaque = NO;
        self.color = [UIColor whiteColor];
    }
    return self;
}

- (void)dealloc
{
    [_color release];
    [super dealloc];
}

- (void)setColor:(UIColor *)color
{
    UIColor *tmpColor = [color retain];
    [_color release];
    _color = tmpColor;
    
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, self.color.CGColor);
    CGContextFillEllipseInRect(context, self.bounds);
}

@end

@interface CCircleColorButton ()

@property (nonatomic, assign) CCircleColorView *colorView;
@property (nonatomic, assign) CGFloat colorRadius;

@end

@implementation CCircleColorButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.colorRadius = self.width/2-1;
    }
    return self;
}

- (CCircleColorView *)colorView
{
    if (_colorView == nil)
    {
        _colorView = [[[CCircleColorView alloc] initWithFrame:CGRectMake(self.width/2-self.colorRadius, self.height/2-self.colorRadius, 2*self.colorRadius, 2*self.colorRadius)] autorelease];
        _colorView.userInteractionEnabled = NO;
        [self addSubview:_colorView];
    }
    return _colorView;
}

- (void)setColor:(UIColor *)color animated:(BOOL)animated
{
    if (animated == NO)
    {
        self.colorView.color = color;
    }
    else
    {
        CCircleColorView *oldView = self.colorView;
        CCircleColorView *newView = [[[CCircleColorView alloc] initWithFrame:oldView.frame] autorelease];
        newView.color = color;
        newView.userInteractionEnabled = NO;
        [self addSubview:newView];
        newView.alpha = 0.0;
        [UIView animateWithDuration:.25 animations:^{
            newView.alpha = 1.0;
            oldView.alpha = 0.0;
        } completion:^(BOOL finished) {
            self.colorView = newView;
            [oldView removeFromSuperview];
        }];
    }
}

- (void)setRadius:(CGFloat)radius animated:(BOOL)animated
{
    _colorRadius = radius;
    
    if (animated == NO)
    {
        self.colorView.frame = CGRectMake(self.width/2-radius, self.height/2-radius, 2*radius, 2*radius);
    }
    else
    {
        CCircleColorView *oldView = self.colorView;
        CCircleColorView *newView = [[[CCircleColorView alloc] initWithFrame:oldView.frame] autorelease];
        newView.color = oldView.color;
        newView.userInteractionEnabled = NO;
        [self addSubview:newView];
        newView.alpha = 0.0;
        [UIView animateWithDuration:.25 animations:^{
            newView.alpha = 1.0;
            newView.frame = CGRectMake(self.width/2-radius, self.height/2-radius, 2*radius, 2*radius);
            oldView.alpha = 0.0;
            oldView.frame = newView.frame;
        } completion:^(BOOL finished) {
            self.colorView = newView;
            [oldView removeFromSuperview];
        }];
    }
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:.9 alpha:.7].CGColor);
    CGContextFillEllipseInRect(context, self.bounds);
    if (self.highlighted)
    {
        CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
        CGContextSetLineWidth(context, 1);
        CGContextStrokeEllipseInRect(context, CGRectInset(self.bounds, 1, 1));
    }
}

@end

@implementation CRoundButton
{
    UILabel *_titleLable;
    UIImageView *_imageView;
    CGFloat _borderSize;
}

+ (id)buttonWithTitle:(NSString *)title
{
    return [[[CRoundButton alloc] initWithTitle:title] autorelease];
}

+ (id)buttonWithNormalImage:(UIImage *)normalImage highlightedImage:(UIImage *)highlightedImage
{
    return [[[CRoundButton alloc] initWithNormalImage:normalImage highlightedImage:highlightedImage] autorelease];
}

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.opaque = NO;
        
        _borderSize = 1;
        self.selectedColor = [UIColor whiteColor];
        self.renderColor = [UIColor colorWithHex:0x3498DB];
        self.cornerRadius = 5;
    }
    return self;
}

- (id)initWithTitle:(NSString *)title
{
    CGSize textSize = [title stringSizeWithFont:[UIFont systemFontOfSize:14]];
    if (self = [self initWithFrame:CGRectMake(0, 0, textSize.width+2*CB_MARGIN, 30)])
    {
        _titleLable = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 30)] autorelease];
        _titleLable.backgroundColor = [UIColor clearColor];
        _titleLable.textAlignment = NSTextAlignmentCenter;
        _titleLable.text = title;
        _titleLable.textColor = self.renderColor;
        _titleLable.font = [UIFont systemFontOfSize:14];
        [self addSubview:_titleLable];
        
        
    }
    return self;
}

- (id)initWithNormalImage:(UIImage *)normalImage highlightedImage:(UIImage *)highlightedImage
{
    if (self = [self initWithFrame:CGRectMake(0, 0, normalImage.size.width+2*CB_MARGIN, normalImage.size.height+2*CB_MARGIN)])
    {
        _imageView = [[[UIImageView alloc] initWithImage:normalImage highlightedImage:highlightedImage] autorelease];
        _imageView.center = self.center;
        [self addSubview:_imageView];
    }
    return self;
}

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    
    _titleLable.textColor = highlighted ? self.selectedColor : self.renderColor;
    _imageView.highlighted = highlighted;
    
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(self.bounds, _borderSize/2, _borderSize/2) cornerRadius:self.cornerRadius];
    CGContextAddPath(context, borderPath.CGPath);
    [self.renderColor set];
    CGContextDrawPath(context, self.highlighted ? kCGPathFillStroke : kCGPathStroke);
}

@end
